package com.tensquare.chat.controller;
/*
 *ClassName:ChatController
 *Description:聊天微服务控制层
 *@author:可爱的大鸭子
 */

import com.tensquare.chat.ChatApplication;
import com.tensquare.chat.pojo.SocketMessage;
import com.tensquare.chat.service.MessageService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private MessageService messageService;

    @MessageMapping("/send")
    public void send(SocketMessage message) throws Exception {
        String sessionId= ChatApplication.sessionMap.get(message.getTo());
        //如果对方没有进入到聊天室，就把消息存到数据库，并发送一个通知notice
        if(sessionId==null||sessionId.equals("")){
            messageService.add(message);
            //发送消息通知。。。
            return;
        }
        System.out.println("发送给的用户："+message.getTo());
        //websocket封装好的在线聊天，将消息发给sessionId对应的用户
        messagingTemplate.convertAndSendToUser(sessionId,"/topic/send",message,createHeaders(sessionId));
        //将消息存到数据库
        messageService.add(message);
    }

    private MessageHeaders createHeaders(String sessionId) {
        SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
        headerAccessor.setSessionId(sessionId);
        headerAccessor.setLeaveMutable(true);
        return headerAccessor.getMessageHeaders();
    }

    //获取聊天记录信息
    @RequestMapping(value = "/getChatList/{from}/{to}",method = RequestMethod.GET)
    public Result getChatList(@PathVariable String from,@PathVariable String to){
        List<SocketMessage> list =messageService.getChatList(from,to);
        return new Result(true, StatusCode.OK,"查询成功",list);
    }

    //查询我的未读消息总数
    @RequestMapping(value = "/getMyMessages/{userId}",method = RequestMethod.GET)
    public Result getMyMessages(@PathVariable String userId){
        Integer num=messageService.getMyMessages(userId);
        return new Result(true,StatusCode.OK,"查询成功",num);
    }

    //返回我的未读消息列表，一个人有多条消息的时候只返回最新的消息，并返回消息的数量
    @RequestMapping(value = "/getFriendsMessages/{userId}",method = RequestMethod.GET)
    public Result getFriendsMessages(@PathVariable String userId){
        List<SocketMessage> list=messageService.findMessages(userId);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    //处理对某人的未读消息变为已读
    @RequestMapping(value = "/setReaded/{from}/{to}",method = RequestMethod.PUT)
    public Result setReaded(@PathVariable String from,@PathVariable String to){
        messageService.setReaded(from,to);    //count是已读的消息数
        return new Result(true,StatusCode.OK,"全部已读");
    }
}
